﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewsAggregatorLibrary.DAL;

namespace NewsAggregatorLibrary
{
    public class CategoriesMember : Categories
    {
        /// <summary>
        /// Retrieves all Categories from the DB.
        /// </summary>
        /// <returns></returns>
        public static CategoriesMemberCollection RetrieveAll()
        {
            try
            {
                return DB.Select().From(Schema).ExecuteAsCustomCollection<Categories, CategoriesCollection, CategoriesMember, CategoriesMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while generating a list of Categories " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a category by ID.
        /// </summary>
        /// <returns></returns>
        public static CategoriesMember RetrieveByID(int id)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.Id).IsEqualTo(id).ExecuteSingle<CategoriesMember>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while a category " + e.Message, e);
            }
        }

        /// <summary>
        /// Adds a new Category to the DB.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static int AddCategory(string name)
        {
            CategoriesMember category = new CategoriesMember();
            try
            {
                category.Name = name;

                category.Save();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while inserting a new category. " + e.Message);

            }
            return category.Id;
        }

        /// <summary>
        /// Updates a Category.
        /// </summary>
        /// <param name="id">The id of the category to update.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static Boolean UpdateFeed(int id, string name)
        {
            Boolean ifUpdate = false;
            try
            {
                CategoriesMember category = RetrieveByID(id);

                if (category != null)
                {
                    DB.Update<Categories>()
                    .Set(Columns.Name).EqualTo(name)
                    .Where(Columns.Id).IsEqualTo(id).Execute();

                    ifUpdate = true;
                }
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while updating a category with id=" + id + ". " + e.Message);
            }
            return ifUpdate;
        }
    }

    public class CategoriesMemberCollection : CategoriesCollection
    {
    }
}
